package com.hongdee.crm.loss.controller;

import com.hongdee.base.entity.ResultEntity;
import com.hongdee.common.util.TokenUtil;
import com.hongdee.crm.loss.entity.TCrmCarLoss;
import com.hongdee.crm.loss.service.TCrmLossService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.Map;

/**
 * Created by dell on 2017/5/5.
 */
@RestController
@RequestMapping("/crm/loss")
public class TCRMLossController {
    @Autowired
    private TCrmLossService tCrmLossService;

    /*
 * 分页
 * */
    @RequestMapping("/page")
    @ResponseBody
    public Map newPage(@RequestParam(name = "uid", defaultValue = "") String uid,
                       @RequestParam(name = "token", defaultValue = "") String token,
                       @RequestParam(name = "start", defaultValue = "0") String start,
                       @RequestParam(name = "limit", defaultValue = "50") String limit,
                       @RequestParam(name = "typeCode", defaultValue = "") String typeCode,
                       @RequestParam(name = "code", defaultValue = "") String code

    ) throws Exception {
        //获取公司ID
        String companyId = TokenUtil.getCompanyId(uid);
        Map map = this.tCrmLossService.newPage(start, limit, typeCode, code, companyId);
        map.put("success", true);
        map.put("msg", "分页查询成功！");
        return map;
    }

    /**
     * 停用
     */
    @RequestMapping("/start")
    @ResponseBody
    public ResultEntity start(@RequestParam(name = "uid", defaultValue = "") String uid,
                              @RequestParam(name = "token", defaultValue = "") String token,
                              @RequestParam(name = "id", defaultValue = "0") String id) throws Exception {
        ResultEntity res = new ResultEntity();
        tCrmLossService.update(id, "1");
        res.setSuccess(true);
        res.setMsg("启动成功！");
        return res;
    }

    /**
     * 停用
     */
    @RequestMapping("/stop")
    @ResponseBody
    public ResultEntity stop(@RequestParam(name = "uid", defaultValue = "") String uid,
                             @RequestParam(name = "token", defaultValue = "") String token,
                             @RequestParam(name = "id", defaultValue = "0") String id) throws Exception {
        ResultEntity res = new ResultEntity();
        tCrmLossService.update(id, "0");
        res.setSuccess(true);
        res.setMsg("停用成功！");
        return res;
    }

    /*
    * 查询
    * */
    @RequestMapping("/info")
    @ResponseBody
    public ResultEntity info(@RequestParam(name = "uid", defaultValue = "") String uid,
                             @RequestParam(name = "token", defaultValue = "") String token,
                             @RequestParam(name = "id", defaultValue = "") String id) throws Exception {
        ResultEntity res = new ResultEntity();
        Map map = tCrmLossService.info(id);
        res.setData(map);
        res.setSuccess(true);
        res.setMsg("查询详情成功");
        return res;
    }

    /**
     * 添加新增
     */
    @RequestMapping("/save")
    @ResponseBody
    public ResultEntity save(@RequestParam(name = "uid", defaultValue = "") String uid,
                             @RequestParam(name = "token", defaultValue = "") String token,
                             @RequestBody TCrmCarLoss ref) throws Exception {

        ref.setCompanyId(TokenUtil.getCompanyId(uid));
        ref.setCreateUser(uid);
        TCrmCarLoss group = tCrmLossService.saveOrUpdate(ref, uid);

        ResultEntity res = new ResultEntity();
        System.out.print("21651586165" + res);
        res.setData(group);
        res.setSuccess(true);
        res.setMsg("保存成功！");
        return res;
    }

    //下拉框
    @RequestMapping("/combo")
    public List combo(@RequestParam(name = "uid", defaultValue = "") String uid,
                      @RequestParam(name = "token", defaultValue = "") String token,
                      @RequestParam(name = "typeCode", defaultValue = "") String typeCode,
                      @RequestParam(name = "companyId", defaultValue = "") String companyId) throws Exception {
        ResultEntity res = new ResultEntity();
        if (StringUtils.isEmpty(companyId)) {
            companyId = TokenUtil.getCompanyId(uid);
        }
        List list = tCrmLossService.combo(typeCode, companyId);
        return list;


    }
}
